package cc.lj.internal.coupons.interfaces.facade;

import cc.lj.internal.core.exception.SysExceptionEnum;
import cc.lj.internal.core.http.HttpResult;
import cc.lj.internal.coupons.application.service.UseTicketCheckAppService;
import cc.lj.internal.coupons.domain.ticket.repository.IUseTicketCheckCacheRepository;
import cc.lj.internal.coupons.interfaces.dto.TicketMainDTO;
import cc.lj.internal.coupons.interfaces.dto.command.DeleteUseTicketCheckCommand;
import cc.lj.internal.coupons.interfaces.dto.command.UseTicketCheckCommand;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * 用券验证规则
 * @author liangxifeng
 * @date 2022-06-14
 */
@RestController
@Slf4j
@Validated
@Api(tags = "用券验证规则接口")
@RequestMapping("useTicket")
public class UseTicketCheckController {
    @Autowired
    private UseTicketCheckAppService checkAppService;

    @Resource(name = "usetTicketCheckCacheRedisRepositoryImpl")
    private IUseTicketCheckCacheRepository cacheRepository;

    /**
     * 用券验证券规则
     * 验证通过返回券表DTO
     * 验证失败返回DTO=NULL
     */
    @PostMapping("/checkRule")
    public HttpResult<TicketMainDTO> checkRule(@Valid @RequestBody UseTicketCheckCommand checkCommand) {
        TicketMainDTO ticketMainDTO = checkAppService.doMain(checkCommand);
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),ticketMainDTO);
    }

    /**
     * 删除redis中所有指定合同的用券数据
     * @param pactNum 合同号
     * Integer 1 删除成功
     */
    @PostMapping("/deleteAllRedisUsedTicketData")
    public HttpResult<Integer> delAllRedisData(@Valid  @NotBlank(message = "入参合同号不可为空") String pactNum) {
        cacheRepository.deleteAll(pactNum);
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),1);
    }

}
